import urllib3
import sys


HEADERS = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/59.0.3071.115 Safari/537.36'
}


def extract_img(h):
    if h[0:4] == 'http':
        return h
    else:
        return 'http://wx3.sinaimg.cn/mw690/' + h


def get_img_urls(input_file):
    for l in open(input_file):
        d = l.strip().split('\t')
        if len(d) != 2:
            continue

        img_urls = [extract_img(h) for h in d[1].split(',')]
        names = [d[0] + '_' + str(i) + '.jpg' for i in range(len(img_urls))]
        yield img_urls, names


def download_img(pool, img_url, save_file):
    buf = pool.request('GET', img_url, headers=HEADERS).data
    with open(save_file, 'wb+') as fd:
        fd.write(buf)


if __name__ == '__main__':
    img_url_file = sys.argv[1]
    out_dir = sys.argv[2]

    img_urls = get_img_urls(img_url_file)

    pm = urllib3.PoolManager()

    for imgs, names in img_urls:
        for i, n in zip(imgs, names):
            download_img(pm, i, out_dir + '/' + n)
